﻿using AutoMapper;
using Equipment.API.DTOs.RequestDto.DefectlocationListRequest;
using Equipment.API.DTOs.ResponseDto.DefectlocationResponse;
using Equipment.Domain.Detectionmethod;
using Equipment.Errorcode;
using Equipment.Errorcode.ErrorCode;
using Equipment.Infrastructure.Interfaces;
using Serilog;

namespace Equipment.API.Application.Service.DetectionmethodService
{
    /// <summary>
    /// 缺陷类型信息服务层
    /// </summary>
    public class DefecttypeService
    {
        public readonly IDefecttypeRepository _defecttypeRepository;

        public readonly IMapper _mapper;

        private readonly ILogger<DefecttypeService> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="defecttypeRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        public DefecttypeService(IDefecttypeRepository defecttypeRepository, IMapper mapper, ILogger<DefecttypeService> logger)
        {
            _defecttypeRepository = defecttypeRepository;
            _mapper = mapper;
            _logger = logger;
        }

        /// <summary>
        /// 缺陷类型下拉框
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult> GetDefecttypeDropdown()
        {
            var defecttypes = await _defecttypeRepository.GetAllAsync();

            var defecttypeList = _mapper.Map<List<DefecttypeDropdownResponseDto>>(defecttypes);

            return ApiResult.Ok(defecttypeList, "获取缺陷类型下拉列表成功！");
        }
        /// <summary>
        /// 添加缺陷类型信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult> AddDefecttype(AddDefecttypeResponseDto param)
        {
            try
            {
                var defecttype = _mapper.Map<Defecttype>(param);
                await _defecttypeRepository.InsertAsync(defecttype);
                return ApiResult.Ok(defecttype, "添加缺陷类型信息成功！");

            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "缺陷类型信息添加错误！");
                return ApiResult.Fail("添加失败"); // 处理异常后返回
            }
        }
        /// <summary>
        /// 获取缺陷信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult> GetDefecttype(GetDefecttypeRequestDto dto, int pageNumber = 1, int pageSize = 4)
        {
            try
            {
            Log.Information("缺陷信息获取成功！");

                var defecttypes = (await _defecttypeRepository.GetAllAsync(a => a.Isdelete == false)).AsQueryable();
                if (dto.Typeencodingname != null)
                {
                    defecttypes = defecttypes.Where(a => a.Typename.Contains(dto.Typeencodingname) || a.Typeencoding.Contains(dto.Typeencodingname));
                }
                if(dto.Status != null)
                {
                    defecttypes = defecttypes.Where(a => a.Status == dto.Status);
                }

                var page = PaginationHelper.Paginate(defecttypes, pageNumber, pageSize);
                return ApiResult.Ok(page, "缺陷信息获取成功！");
            }
            catch (Exception ex)
            {
                Log.Error(ex, "获取缺陷类型信息时发生错误。");
                return ApiResult.Fail("获取缺陷类型信息时发生错误。");
                throw;
            }
        }
        
        /// <summary>
        /// 修改缺陷类型信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult> UpdateDefecttype(UpdateDefecttypeRequestDto dto)
        {
            try
            {
                Log.Information("开始更新缺陷信息！");
                var defecttype = await _defecttypeRepository.GetAsync(a => a.Defecttypeid == dto.Defecttypeid);
                if (defecttype == null)
                {
                    return ApiResult.Fail("未找到该缺陷类型信息！");
                }
                _mapper.Map(dto, defecttype);
                await _defecttypeRepository.UpdateAsync(defecttype);

                return ApiResult.Ok(defecttype, "缺陷信息更新成功！");
            }
            catch (Exception ex)
            {

                Log.Error(ex, "更新缺陷类型信息时发生错误。");
                return ApiResult.Fail("更新缺陷类型信息时发生错误。");
            }
        }
        /// <summary>
        /// 删除缺陷类型信息
        /// </summary>
        /// <param name="Defecttypeid"></param>
        /// <returns></returns>
        public async Task<ApiResult> DeleteDefecttype(int Defecttypeid)
        {
            try
            {
            Log.Information("开始删除缺陷类型操作！");
            var defecttype = await _defecttypeRepository.GetAsync(a => a.Defecttypeid == Defecttypeid);
            if (defecttype == null)
            {
                return ApiResult.Fail("未找到该缺陷类型信息！");
            }
            defecttype.Isdelete = true;
            await _defecttypeRepository.UpdateAsync(defecttype);

            return ApiResult.Ok(defecttype, "删除缺陷类型信息成功！");

            }
            catch (Exception ex)
            {
                Log.Error(ex, "删除缺陷类型信息时发生错误。");
                return ApiResult.Fail("删除缺陷类型信息时发生错误。");
            }
        }




    }
}
